package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsGoods;
import com.itqf.bean.DtsRole;
import com.itqf.dto.DtsSearchDto;
import com.itqf.mapper.DtsGoodsMapper;
import com.itqf.service.DtsGoodsService;
import com.itqf.service.DtsOrderService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AdminResponseCode;
import com.itqf.utils.AdminResponseUtil;
import com.itqf.utils.AuthSupport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/17 11:33
 * description:商品列表
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/goods")
public class AdminGoodsController {
    //商品列表显示所有admin/goods/list
    @Autowired
    private DtsGoodsService dtsGoodsService;

    public Logger logger = LoggerFactory.getLogger(AdminGoodsController.class);

    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品列表"}, button = "商品查看")
    @GetMapping("list")
    public Object list(DtsSearchDto dto) {
        logger.debug("[角色列表]角色列表查询参数是", dto);
        Map goods = dtsGoodsService.findGoodsByPage(dto);
        logger.debug("[角色列表]分页查询数据为", goods);
        return ResponseUtil.ok(goods);
    }





    //商品列表添加/admin/goods/catAndBrand   goods/create
    @RequiresPermissions("admin:goods:create")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品"}, button = "商品添加")
    @PostMapping("create")
    public Object catAndBrand(@RequestBody DtsGoods goods) {
        //1·用户名是否在数据库存在的判断
        String goodsname = goods.getName();
        if (goodsname == null || goodsname == "") {
            logger.warn("[保存角色]接收到的用户名为null,无法注册!");
            return ResponseUtil.fail();
        }
        boolean hasGoodsName = dtsGoodsService.checkGoodsName(goodsname);
        if (hasGoodsName) {
            logger.error("[保存管理员]用户名:{},已经被注册:{}", goodsname);
            return AdminResponseUtil.fail(AdminResponseCode.ADMIN_NAME_EXIST);
        }
        //2·保存用户
        goods = dtsGoodsService.saveGoods(goods);
        //3·返回结果
        logger.info("[保存角色]接口调用结束,获得回显的数据为:{}", goods);
        return ResponseUtil.ok(goods);
    }






    //商品列表更新/admin/goods/detail
    @RequiresPermissions("admin:goods:detail")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "商品更新")
    @PostMapping("detail")
    public Object detail(@RequestBody DtsGoods goods) {
        //修改数据
        int rows = dtsGoodsService.updateGoods(goods);
        //返回结果
        if (rows == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(goods);
    }




    //商品列表删除/admin/goods/delete
    @RequiresPermissions("admin:goods:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品删除"}, button = "商品删除")
    @PostMapping("delete")
    public Object delete(@RequestBody DtsGoods dtsGoods){
        logger.debug("[删除商品]传入的id为:{}",dtsGoods.getId());
        int rows = dtsGoodsService.removeGoodsById(dtsGoods.getId());
        if(rows == 0){
            return ResponseUtil.fail();
        }
        logger.info("[删除商品]删除影响行数为",rows);
        return ResponseUtil.ok();
}
}
